<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx反向代理详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4, h5 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
        }
        .code-block {
            background-color: #2d2d2d;
            color: #f8f8f2;
            border-radius: 6px;
            font-family: 'Courier New', Courier, monospace;
        }
        .section-divider {
            border-bottom: 1px solid #e2e8f0;
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 3.5em;
            line-height: 0.9;
            margin-right: 0.1em;
            color: #4f46e5;
            font-weight: bold;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6">Nginx反向代理全面解析</h1>
                <p class="text-xl md:text-2xl mb-8 max-w-3xl">探索高性能Web服务器的核心代理技术</p>
                <div class="flex space-x-4">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-server mr-2"></i>负载均衡</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-shield-alt mr-2"></i>安全防护</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-bolt mr-2"></i>性能优化</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-indigo-600 mr-3"></div>
                <h2 class="text-2xl font-bold">Nginx反向代理</h2>
            </div>
            <p class="drop-cap text-gray-700 mb-6">Nginx反向代理是指Nginx作为中介接收客户端的请求，并将这些请求转发到后端服务器进行处理，然后将后端服务器的响应返回给客户端。与传统的正向代理不同，反向代理是将请求代理到内网或不同服务器上，隐藏了实际处理请求的服务器。反向代理通常用于负载均衡、提高性能、安全性和高可用性等场景。</p>
            
            <div class="grid md:grid-cols-2 gap-6 my-8">
                <div class="card-hover bg-white p-6 rounded-lg shadow-md border border-gray-100">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 text-indigo-600 flex items-center justify-center mr-3">
                            <i class="fas fa-arrow-right"></i>
                        </div>
                        <h3 class="text-lg font-semibold">正向代理</h3>
                    </div>
                    <p class="text-gray-600">客户端配置代理，代理服务器代表客户端访问外部网络资源。</p>
                </div>
                <div class="card-hover bg-white p-6 rounded-lg shadow-md border border-gray-100">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-purple-100 text-purple-600 flex items-center justify-center mr-3">
                            <i class="fas fa-arrow-left"></i>
                        </div>
                        <h3 class="text-lg font-semibold">反向代理</h3>
                    </div>
                    <p class="text-gray-600">客户端无需知道后端服务器的存在，所有请求都由反向代理服务器处理。</p>
                </div>
            </div>
            
            <div class="bg-blue-50 border-l-4 border-blue-500 p-4 my-6 rounded">
                <div class="flex">
                    <div class="flex-shrink-0 text-blue-500">
                        <i class="fas fa-info-circle"></i>
                    </div>
                    <div class="ml-3">
                        <h3 class="text-sm font-medium text-blue-800">反向代理特点</h3>
                        <div class="mt-2 text-sm text-blue-700">
                            <ul class="list-disc pl-5 space-y-1">
                                <li>隐藏真实服务器，增强系统安全性</li>
                                <li>实现负载均衡，提高并发处理能力</li>
                                <li>缓存静态内容，提升响应速度</li>
                                <li>作为防火墙，保护后端服务器</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Diagram Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-indigo-600 mr-3"></div>
                <h2 class="text-2xl font-bold">工作原理</h2>
            </div>
            <div class="mermaid text-center">
                sequenceDiagram
                    participant C as 客户端
                    participant N as Nginx
                    participant B as 后端服务器
                    C->>N: 发送请求
                    N->>B: 转发请求
                    B->>N: 返回响应
                    N->>C: 返回响应
            </div>
            <div class="grid md:grid-cols-4 gap-4 mt-8">
                <div class="text-center p-4">
                    <div class="w-16 h-16 bg-indigo-100 rounded-full flex items-center justify-center mx-auto mb-3">
                        <i class="fas fa-paper-plane text-indigo-600 text-xl"></i>
                    </div>
                    <h3 class="font-semibold">1. 客户端发送请求</h3>
                </div>
                <div class="text-center p-4">
                    <div class="w-16 h-16 bg-purple-100 rounded-full flex items-center justify-center mx-auto mb-3">
                        <i class="fas fa-exchange-alt text-purple-600 text-xl"></i>
                    </div>
                    <h3 class="font-semibold">2. Nginx处理请求</h3>
                </div>
                <div class="text-center p-4">
                    <div class="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mx-auto mb-3">
                        <i class="fas fa-reply text-blue-600 text-xl"></i>
                    </div>
                    <h3 class="font-semibold">3. 后端服务器响应</h3>
                </div>
                <div class="text-center p-4">
                    <div class="w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-3">
                        <i class="fas fa-check-circle text-green-600 text-xl"></i>
                    </div>
                    <h3 class="font-semibold">4. 客户端收到响应</h3>
                </div>
            </div>
        </section>

        <!-- Applications Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-indigo-600 mr-3"></div>
                <h2 class="text-2xl font-bold">应用场景</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="card-hover bg-white p-6 rounded-lg shadow-md border border-gray-100">
                    <div class="flex items-start mb-4">
                        <div class="w-10 h-10 rounded-full bg-yellow-100 text-yellow-600 flex items-center justify-center mr-3 flex-shrink-0">
                            <i class="fas fa-balance-scale"></i>
                        </div>
                        <div>
                            <h3 class="text-lg font-semibold mb-2">负载均衡</h3>
                            <p class="text-gray-600">将客户端请求分发到多个后端服务器，提升系统整体处理能力。</p>
                            <div class="mt-3">
                                <span class="inline-block bg-gray-100 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2">轮询</span>
                                <span class="inline-block bg-gray-100 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2">最少连接</span>
                                <span class="inline-block bg-gray-100 rounded-full px-3 py-1 text-sm font-semibold text-gray-700">IP哈希</span>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="card-hover bg-white p-6 rounded-lg shadow-md border border-gray-100">
                    <div class="flex items-start mb-4">
                        <div class="w-10 h-10 rounded-full bg-green-100 text-green-600 flex items-center justify-center mr-3 flex-shrink-0">
                            <i class="fas fa-tachometer-alt"></i>
                        </div>
                        <div>
                            <h3 class="text-lg font-semibold mb-2">缓存加速</h3>
                            <p class="text-gray-600">缓存静态内容减少后端负载，大幅提升访问性能。</p>
                            <div class="mt-3">
                                <span class="inline-block bg-gray-100 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2">静态资源</span>
                                <span class="inline-block bg-gray-100 rounded-full px-3 py-1 text-sm font-semibold text-gray-700">动态缓存</span>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="card-hover bg-white p-6 rounded-lg shadow-md border border-gray-100">
                    <div class="flex items-start mb-4">
                        <div class="w-10 h-10 rounded-full bg-red-100 text-red-600 flex items-center justify-center mr-3 flex-shrink-0">
                            <i class="fas fa-lock"></i>
                        </div>
                        <div>
                            <h3 class="text-lg font-semibold mb-2">TLS/SSL终端代理</h3>
                            <p class="text-gray-600">在Nginx处进行SSL/TLS协议的加密与解密，减轻后端负担。</p>
                        </div>
                    </div>
                </div>
                
                <div class="card-hover bg-white p-6 rounded-lg shadow-md border border-gray-100">
                    <div class="flex items-start mb-4">
                        <div class="w-10 h-10 rounded-full bg-blue-100 text-blue-600 flex items-center justify-center mr-3 flex-shrink-0">
                            <i class="fas fa-comments"></i>
                        </div>
                        <div>
                            <h3 class="text-lg font-semibold mb-2">WebSocket代理</h3>
                            <p class="text-gray-600">支持WebSocket协议升级，适用于实时应用通信。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Configuration Section -->
        <section class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-indigo-600 mr-3"></div>
                <h2 class="text-2xl font-bold">配置示例</h2>
            </div>
            
            <div class="mb-8">
                <h3 class="text-xl font-semibold mb-4">基本反向代理配置</h3>
                <div class="code-block p-4 overflow-x-auto">
                    <pre><code>server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}</code></pre>
                </div>
            </div>
            
            <div>
                <h3 class="text-xl font-semibold mb-4">负载均衡配置</h3>
                <div class="code-block p-4 overflow-x-auto">
                    <pre><code>http {
    upstream backend_servers {
        server backend1.example.com;
        server backend2.example.com;
        server backend3.example.com;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend_servers;
        }
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Optimization Section -->
        <section>
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-indigo-600 mr-3"></div>
                <h2 class="text-2xl font-bold">性能优化</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white p-6 rounded-lg shadow-md border border-gray-100">
                    <h3 class="text-xl font-semibold mb-4"><i class="fas fa-database text-indigo-500 mr-2"></i>缓存优化</h3>
                    <p class="text-gray-600 mb-4">通过优化Nginx的缓存策略，可以有效提升系统性能，减轻后端服务器负担。</p>
                    <ul class="list-disc pl-5 space-y-2 text-gray-600">
                        <li>设置合适的缓存过期时间</li>
                        <li>配置缓存大小限制</li>
                        <li>启用静态资源缓存</li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-md border border-gray-100">
                    <h3 class="text-xl font-semibold mb-4"><i class="fas fa-plug text-green-500 mr-2"></i>连接池配置</h3>
                    <p class="text-gray-600 mb-4">通过keepalive指令维持与后端服务器的持久连接，减少TCP连接开销。</p>
                    <div class="bg-gray-100 p-3 rounded">
                        <code class="text-sm">keepalive 32;</code>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-md border border-gray-100">
                    <h3 class="text-xl font-semibold mb-4"><i class="fas fa-compress-arrows-alt text-blue-500 mr-2"></i>Gzip压缩</h3>
                    <p class="text-gray-600 mb-4">减少网络带宽消耗，提高传输速度。</p>
                    <div class="code-block p-3 rounded text-sm">
                        <pre><code>gzip on;
gzip_types text/plain application/json application/javascript text/css;</code></pre>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-lg shadow-md border border-gray-100">
                    <h3 class="text-xl font-semibold mb-4"><i class="fas fa-heartbeat text-red-500 mr-2"></i>健康检查</h3>
                    <p class="text-gray-600 mb-4">确保请求只会转发到健康的后端服务器。</p>
                    <div class="bg-gray-100 p-3 rounded">
                        <code class="text-sm">health_check interval=5s;</code>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold">技术小馆</h3>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="hover:text-indigo-300 transition-colors">
                        <i class="fas fa-external-link-alt mr-2"></i>http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="mt-6 text-center text-gray-400 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            fontFamily: 'Noto Sans SC, sans-serif'
        });
    </script>
</body>
</html>